<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use zgldh\QiniuStorage\QiniuStorage;
use App\HairColor;
use App\Hair;
use App\Color;
use Input;
class ColorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $colors = Color::select('colortypeid','colorname','samplethumb','colorvalue')->paginate('20');
        return view('admin.color.index',compact('colors'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.color.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证
        $this -> validate($request,[
            'colorvalue'=>'max:7',
        ],[
            'colorvalue.max'=>'颜色值超过最大值',
        ]);
        $colorvalue  =  trim(Input::get('colorvalue'));
        $colorname   =  trim(Input::get('colorname'));
        //将数据插入
        //先判断是否有图片
         if ($request->hasFile('file')) {
            $file = $request->file('file');
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $extension = strtolower($extension);
            if (!in_array($extension, ['png','jpg']))
            return back()->with(['info' => '需要png类型或者jpg类型']);
            // 初始化
            $disk = QiniuStorage::disk('qiniu');
            $newName = 'hair/'.md5(date('Y-m-d H:i:s') . $clientName) . "." . $extension;
            // 上传到七牛
            $bool = $disk->put($newName, file_get_contents($file->getRealPath()));
            if($bool){
                //将数据插入数据库
                $res = Color::create(['colorname'=>$colorname,'samplethumb'=>$newName,'colorvalue'=>$colorvalue]);
                if($res)
                return back() -> with(['info'=>'数据添加成功']);
                return back() -> with(['info'=>'数据添加失败']);


            }else{
                return back() -> with(['info'=>'存入七牛失败;']);

            }
        }
        return back() -> with(['info'=>'没有图片']);
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id获取到数据
        $datas = Color::where('colortypeid',$id)->first();
        return view('admin.color.edit',compact('datas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //进行表单验证
        $colorname = trim(Input::get('colorname'));
        $colorvalue = trim(Input::get('colorvalue'));
        $samplethumb = Input::get('samplethumb');
        if(!($request -> hasFile('file'))){
             $res = Color::where('colortypeid',$id)->update(['colorname'=>$colorname,'colorvalue'=>$colorvalue]);
            if($res === false)
            return back() -> with(['info'=>'更新不成功']);
            return Redirect('hair')->with(['info'=>'更新成功']);
        }else{
             //说明有缩略图传来;
                $file = Input::file('file');
                //将数据组装成数据进行数据判断,还有文件
                $extension = $file->getClientOriginalExtension();
                $extension = strtolower($extension);
                if (!in_array($extension, ['png','jpg']))
                return back()->with(['info' => '需要png类型或者jpg类型']);
                //首先获取它的扩展名
                $clientName = $file->getClientOriginalName();
                $disk = QiniuStorage::disk('qiniu');
                $colorthumb = 'hair/'.md5(date('Y-m-d H:i:s') . $clientName) . "." . $extension;
                $bool = $disk->put($colorthumb, file_get_contents($file->getRealPath()));
                if ($bool) {
                    //将数据插入数据库
                    $res = Color::where('colortypeid',$id)->update(['colorname'=>$colorname,'samplethumb'=>$colorthumb,'colorvalue'=>$colorvalue]);
                    if ($res){
                        $delete   = $disk -> delete($samplethumb);
                        if($delete)
                        return back() -> with(['info' => '更新成功']);
                        return back() -> with(['info' => '更新成功,但原图片删除失败,地址是:'.$samplethumb]);
                    }else{
                        return back() -> with(['info' => '更新失败,图片已经传到七牛云，地址是:'.$colorthumb]);
                    }
                } else {
                    return back()->with(['info' => '没有图片传到七牛，请重新上传']);
                }
            }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取id
        $have = HairColor::where('colortypeid',$id)->first();
        if(empty($have)){
        $datas = Color::where('colortypeid',$id)->first();
        $samplethumb = $datas['samplethumb'];
        $disk = QiniuStorage::disk('qiniu');
        $res = Color::where('colortypeid',$id)->delete();
        if($res){
            $bool = $disk->delete($samplethumb);
            if($bool){
                return Response() -> json(['status'=>'success','msg'=>'删除成功']);
            }else{
                return Response() -> json(['status'=>'error','msg'=>'删除七牛图片失败地址:'.$samplethumb]);
            }
        }else{
            return Response() -> json(['status'=>'error','msg'=>'删除失败']);
        }


        }else{
            return Response() -> json(['status'=>'havechild','msg'=>'下面有数据']);
        }

    }
}
